package com.boot.ys.mybatisplus.controller;


import com.boot.ys.mybatisplus.entity.Abc;
import com.boot.ys.mybatisplus.result.Result;
import com.boot.ys.mybatisplus.service.AbcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.implementation.bind.annotation.Pipe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yang Sheng
 * @since 2021-11-18
 */
@RestController
@RequestMapping("/abc")
@Api(tags = "测试abc")
public class AbcController {

    private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(9);

    private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();

    private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(16, 16, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);

    @Autowired
    private AbcService abcService;

    public Result test(){
        return Result.ok(abcService.test());
    }

    //        CompletableFuture<Object> completableFuture = new CompletableFuture();

    @ApiOperation(value = "测试futureTask异步调用")
    @PostMapping("/testFuture")
    public Object futureTask(){

        System.out.println("开始了"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

		Callable<List> getCommunityCallable1 = () -> {
		    System.out.println("第1步");
		    List<String> list  = new ArrayList<>();
		    list.add("aa");
		    list.add("bb");
		    Thread.sleep(4000);
            System.out.println("第1步开始了"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		    return list;
        };
        FutureTask<List> getgetCommunityTask1 = new FutureTask<>(getCommunityCallable1);
        executorService.submit(getgetCommunityTask1);

        Callable<List> getCommunityCallable2 = () -> {
            System.out.println("第2步");
            List<String> list  = new ArrayList<>();
            list.add("cc");
            list.add("dd");
            Thread.sleep(5000);
            System.out.println("第2步开始了"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return list;
        };
        FutureTask<List> getgetCommunityTask2 = new FutureTask<>(getCommunityCallable2);
        executorService.submit(getgetCommunityTask2);

        Callable<List> getCommunityCallable3 = () -> {
            System.out.println("第3步");
            List<String> list  = new ArrayList<>();
            list.add("ee");
            list.add("ff");
            System.out.println("第3步开始了"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return list;
        };
        FutureTask<List> getgetCommunityTask3 = new FutureTask<>(getCommunityCallable3);
        executorService.submit(getgetCommunityTask3);

        Map<String, Object> data = new HashMap<>();
        try {
            data.put("getgetCommunityTask1",getgetCommunityTask1.get());
            System.out.println("第一次调用");
            System.out.println("第一次调用开始了"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            data.put("getgetCommunityTask2",getgetCommunityTask2.get());
            System.out.println("第二次调用");
            System.out.println("第二次调用开始了"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            data.put("getgetCommunityTask3",getgetCommunityTask3.get());
            System.out.println("第三次调用");
            System.out.println("第三次调用开始了"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.ok(data);



    }


}
